package com.future.mayi.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.app.App;
import com.future.mayi.model.ExpressListModel;
import com.future.mayi.response.ExpressListResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 填写寄件物流
 */
public class EdtExpressActivity extends BaseTitleActivity {
    @BindView(R.id.ll_company)
    LinearLayout llCompany;
    @BindView(R.id.tv_num)
    EditText tvNum;
    @BindView(R.id.tv_enter)
    TextView tvEnter;
    @BindView(R.id.tv_company)
    TextView tvCompany;

    private String orderId;
    private String goodsIds;
    private boolean recycle;

    public static void startActivity(Context context, String orderId) {
        startActivity(context, orderId, false);
    }

    public static void startActivity(Context context, String orderId, boolean recycle) {
        Intent intent = new Intent(context, EdtExpressActivity.class);
        intent.putExtra("orderId", orderId);
        intent.putExtra("recycle", recycle);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, String orderId, boolean recycle,String goodsIds) {
        Intent intent = new Intent(context, EdtExpressActivity.class);
        intent.putExtra("orderId", orderId);
        intent.putExtra("recycle", recycle);
        intent.putExtra("goodsIds", goodsIds);
        context.startActivity(intent);
    }

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("填写寄件物流");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_edt_express);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initDates() {
        orderId = getIntent().getStringExtra("orderId");
        goodsIds = getIntent().getStringExtra("goodsIds");
        recycle = getIntent().getBooleanExtra("recycle", false);
        getIndexExpressList();
    }

    @Override
    protected void initListener() {

    }


    @OnClick({R.id.ll_company, R.id.tv_enter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_company:
                selectCompany();
                break;
            case R.id.tv_enter:
                tvEnter.setEnabled(false);
                if (TextUtils.isEmpty(tvCompany.getText())) {
                    toast("请选择快递公司");
                    tvEnter.setEnabled(true);
                    return;
                }
                if (TextUtils.isEmpty(tvNum.getText())) {
                    toast("填写物流单号");
                    tvEnter.setEnabled(true);
                    return;
                }
                enter();
                break;
        }
    }


    private void enter() {
        showLoadingDialog();
        new HttpRequest<DefaultResponse>()
                .with(this).setApiCode(recycle ? ApiCst.addExpressConsignorder : ApiCst.addExpressOtherorder)
                .addParam("token", App.getToken(mContext))
                .addParam("order_id", orderId)
                .addParam("order_goods_id", goodsIds)
                .addParam("express_name", tvCompany.getText().toString())
                .addParam("express_no", tvNum.getText().toString())
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        dissmissLoadingDialog();
                        toast(response.message);
                        finish();
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        toast(message);
                        dissmissLoadingDialog();
                        tvEnter.setEnabled(true);
                    }
                }).start(new DefaultResponse());
    }


    private List<String> companyList;
    private List<ExpressListModel> list;
    private OptionsPickerView companyDialog;

    private void selectCompany() {
        if (companyList == null) {
            getIndexExpressList();
            toast("正在获取快递列表");
            return;
        }
        if (companyDialog == null) return;
        companyDialog.show();
    }


    public void getIndexExpressList() {
        new HttpRequest<ExpressListResponse>()
                .with(this).setApiCode(ApiCst.getIndexExpressList)
                .addParam("token", App.getToken(mContext))
                .setListener(new HttpRequest.OnNetworkListener<ExpressListResponse>() {
                    @Override
                    public void onSuccess(ExpressListResponse response) {
                        list = response.list;
                        if (list == null || list.size() == 0) {
                            companyList = new ArrayList<>(1);
                        } else {
                            companyList = new ArrayList<>(list.size());
                            for (ExpressListModel expressListModel : list) {
                                companyList.add(expressListModel.getExpress_name());
                            }
                        }
                        if (companyDialog == null) {
                            companyDialog = new OptionsPickerView.Builder(EdtExpressActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
                                @Override
                                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                                    dissmissLoadingDialog();
                                    tvCompany.setText(companyList.get(options1));
                                }
                            }).setCancelColor(getResources().getColor(R.color.main_text_color))
                                    .setSubmitColor(getResources().getColor(R.color.main_color))
                                    .setContentTextSize(20)
                                    .setTitleText("请选择快递公司")
                                    .build();
                        }
                        companyDialog.setPicker(companyList);
                    }

                    @Override
                    public void onFail(String message, ExpressListResponse response) {
                        toast(message);
                    }
                }).start(new ExpressListResponse());

    }


}
